# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

load("@rules_rust//rust:defs.bzl", "rust_doc", "rust_library", "rust_test")
load("//rules:ujson.bzl", "ujson_rust")

package(default_visibility = ["//visibility:public"])

ujson_rust(
    name = "e2e_command",
    srcs = ["//sw/device/lib/testing/json:command"],
    defines = ["opentitanlib=crate"],
)

ujson_rust(
    name = "gpio",
    srcs = ["//sw/device/lib/testing/json:gpio"],
    defines = ["opentitanlib=crate"],
)

ujson_rust(
    name = "pinmux_config",
    srcs = ["//sw/device/lib/testing/json:pinmux_config"],
    defines = ["opentitanlib=crate"],
)

ujson_rust(
    name = "spi_passthru",
    srcs = ["//sw/device/lib/testing/json:spi_passthru"],
    defines = ["opentitanlib=crate"],
)

filegroup(
    name = "config",
    srcs = glob(["src/app/config/*.json"]),
)

rust_library(
    name = "opentitanlib",
    srcs = [
        "src/app/command.rs",
        "src/app/config/mod.rs",
        "src/app/config/structs.rs",
        "src/app/mod.rs",
        "src/backend/cw310.rs",
        "src/backend/hyperdebug.rs",
        "src/backend/mod.rs",
        "src/backend/proxy.rs",
        "src/backend/ti50emulator.rs",
        "src/backend/ultradebug.rs",
        "src/backend/verilator.rs",
        "src/bootstrap/eeprom.rs",
        "src/bootstrap/legacy.rs",
        "src/bootstrap/mod.rs",
        "src/bootstrap/primitive.rs",
        "src/bootstrap/rescue.rs",
        "src/chip/alert.rs",
        "src/chip/boolean.rs",
        "src/chip/mod.rs",
        "src/chip/autogen/mod.rs",
        "src/crypto/mod.rs",
        "src/crypto/rsa.rs",
        "src/crypto/sha256.rs",
        "src/dif/lc_ctrl.rs",
        "src/dif/rstmgr.rs",
        "src/dif/mod.rs",
        "src/image/image.rs",
        "src/image/manifest.rs",
        "src/image/manifest_def.rs",
        "src/image/mod.rs",
        "src/io/eeprom.rs",
        "src/io/emu.rs",
        "src/io/gpio.rs",
        "src/io/i2c.rs",
        "src/io/ioexpander.rs",
        "src/io/jtag.rs",
        "src/io/mod.rs",
        "src/io/spi.rs",
        "src/io/uart.rs",
        "src/lib.rs",
        "src/otp/alert_handler.rs",
        "src/otp/alert_handler_regs.rs",
        "src/otp/lc_state.rs",
        "src/otp/mod.rs",
        "src/otp/otp_img.rs",
        "src/proxy/errors.rs",
        "src/proxy/handler.rs",
        "src/proxy/mod.rs",
        "src/proxy/protocol.rs",
        "src/proxy/socket_server.rs",
        "src/spiflash/flash.rs",
        "src/spiflash/mod.rs",
        "src/spiflash/sfdp.rs",
        "src/test_utils/bootstrap.rs",
        "src/test_utils/e2e_command.rs",
        "src/test_utils/epmp.rs",
        "src/test_utils/gpio.rs",
        "src/test_utils/spi_passthru.rs",
        "src/test_utils/init.rs",
        "src/test_utils/load_bitstream.rs",
        "src/test_utils/mod.rs",
        "src/test_utils/rpc.rs",
        "src/test_utils/status.rs",
        "src/tpm/access.rs",
        "src/tpm/driver.rs",
        "src/tpm/mod.rs",
        "src/tpm/status.rs",
        "src/transport/common/mod.rs",
        "src/transport/common/fpga.rs",
        "src/transport/common/uart.rs",
        "src/transport/cw310/gpio.rs",
        "src/transport/cw310/mod.rs",
        "src/transport/cw310/spi.rs",
        "src/transport/cw310/usb.rs",
        "src/transport/errors.rs",
        "src/transport/hyperdebug/c2d2.rs",
        "src/transport/hyperdebug/dfu.rs",
        "src/transport/hyperdebug/gpio.rs",
        "src/transport/hyperdebug/i2c.rs",
        "src/transport/hyperdebug/mod.rs",
        "src/transport/hyperdebug/spi.rs",
        "src/transport/hyperdebug/ti50.rs",
        "src/transport/ioexpander/mod.rs",
        "src/transport/ioexpander/sx1503.rs",
        "src/transport/mod.rs",
        "src/transport/proxy/emu.rs",
        "src/transport/proxy/gpio.rs",
        "src/transport/proxy/i2c.rs",
        "src/transport/proxy/mod.rs",
        "src/transport/proxy/spi.rs",
        "src/transport/proxy/uart.rs",
        "src/transport/ti50emulator/emu.rs",
        "src/transport/ti50emulator/gpio.rs",
        "src/transport/ti50emulator/i2c.rs",
        "src/transport/ti50emulator/mod.rs",
        "src/transport/ti50emulator/spi.rs",
        "src/transport/ti50emulator/uart.rs",
        "src/transport/ultradebug/gpio.rs",
        "src/transport/ultradebug/mod.rs",
        "src/transport/ultradebug/mpsse.rs",
        "src/transport/ultradebug/spi.rs",
        "src/transport/ultradebug/uart.rs",
        "src/transport/verilator/mod.rs",
        "src/transport/verilator/gpio.rs",
        "src/transport/verilator/subprocess.rs",
        "src/transport/verilator/transport.rs",
        "src/transport/verilator/uart.rs",
        "src/uart/console.rs",
        "src/uart/mod.rs",
        "src/util/bigint.rs",
        "src/util/bitfield.rs",
        "src/util/file.rs",
        "src/util/mod.rs",
        "src/util/nix.rs",
        "src/util/num_de.rs",
        "src/util/parse_int.rs",
        "src/util/present.rs",
        "src/util/rom_detect.rs",
        "src/util/unknown.rs",
        "src/util/usb.rs",
        "src/util/usr_access.rs",
        "src/util/voltage.rs",
        "src/util/openocd.rs",
        "src/util/printer.rs",
    ] + select({
        "//sw/device:is_english_breakfast": [],
        "//conditions:default": [
            "src/chip/autogen/earlgrey.rs",
            "src/test_utils/pinmux_config.rs",
        ],
    }),
    compile_data = [
        ":config",
        ":gpio",
        ":e2e_command",
        ":pinmux_config",
        ":spi_passthru",
        "@hyperdebug_firmware//file",
    ],
    crate_features = [
        "include_hyperdebug_firmware",
    ] + select({
        "//sw/device:is_english_breakfast": ["english_breakfast"],
        "//conditions:default": [],
    }),
    proc_macro_deps = [
        "//sw/host/opentitanlib/opentitantool_derive",
        "@crate_index//:serde_derive",
        "@lowrisc_serde_annotate//annotate_derive:annotate_derive",
    ],
    rustc_env = {
        "e2e_command": "$(location :e2e_command)",
        "gpio": "$(location :gpio)",
        "pinmux_config": "$(location :pinmux_config)",
        "spi_passthru": "$(location :spi_passthru)",
        "hyperdebug_firmware": "$(location @hyperdebug_firmware//file)",
    },
    deps = [
        "//sw/host/opentitanlib/bindgen",
        "@crate_index//:anyhow",
        "@crate_index//:arrayvec",
        "@crate_index//:bitflags",
        "@crate_index//:bitvec",
        "@crate_index//:byteorder",
        "@crate_index//:chrono",
        "@crate_index//:crc",
        "@crate_index//:deser-hjson",
        "@crate_index//:directories",
        "@crate_index//:env_logger",
        "@crate_index//:erased-serde",
        "@crate_index//:hex",
        "@crate_index//:humantime",
        "@crate_index//:humantime-serde",
        "@crate_index//:indicatif",
        "@crate_index//:lazy_static",
        "@crate_index//:log",
        "@crate_index//:memoffset",
        "@crate_index//:mio",
        "@crate_index//:mio-signals",
        "@crate_index//:nix",
        "@crate_index//:num-bigint-dig",
        "@crate_index//:num-traits",
        "@crate_index//:num_enum",
        "@crate_index//:rand",
        "@crate_index//:raw_tty",
        "@crate_index//:regex",
        "@crate_index//:rsa",
        "@crate_index//:rusb",
        "@crate_index//:serde",
        "@crate_index//:serde_json",
        "@crate_index//:serialport",
        "@crate_index//:sha2",
        "@crate_index//:shellwords",
        "@crate_index//:structopt",
        "@crate_index//:thiserror",
        "@crate_index//:typetag",
        "@crate_index//:zerocopy",
        "@lowrisc_safe_ftdi//:safe_ftdi",
        "@lowrisc_serde_annotate//:serde_annotate",
    ],
)

rust_test(
    name = "opentitanlib_test",
    compile_data = [
        ":e2e_command",
        ":gpio",
        ":pinmux_config",
        "@hyperdebug_firmware//file",
        "src/bootstrap/simple.bin",
        "src/spiflash/SFDP_MX66L1G.bin",
    ],
    crate = ":opentitanlib",
    data = [
        "src/image/testdata/hello.txt",
        "src/image/testdata/manifest.hjson",
        "src/image/testdata/manifest_missing.hjson",
        "src/image/testdata/test_image.bin",
        "src/image/testdata/world.txt",
        "src/otp/testdata/lc_ctrl_state.hjson",
        "src/otp/testdata/otp_ctrl_img_dev.hjson",
        "src/otp/testdata/otp_ctrl_mmap.hjson",
        "src/otp/testdata/output.vmem",
    ],
    rustc_env = {
        "e2e_command": "$(location :e2e_command)",
        "gpio": "$(location :gpio)",
        "pinmux_config": "$(location :pinmux_config)",
    },
)

rust_doc(
    name = "opentitanlib_doc",
    crate = ":opentitanlib",
)
